package com.zuorenke.vertx.factory;

import com.zuorenke.help.SpringUtils;
import io.vertx.core.Handler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;

/**
 * @author Z.R.K
 * @description
 * @create 2019-05-10 16:29:21
 **/
@Slf4j
public abstract class HandlerFactory {
	public static Handler builder(Class<? extends Handler> clazz){
		Handler handler = null;
		try{
			handler = SpringUtils.getBean(clazz);
		}catch (BeansException e){
			log.info("{} bean not found, create it", clazz.getName());
		}
		if(handler == null){
			try {
				handler = clazz.newInstance();
			} catch (InstantiationException e) {
				log.error("InstantiationException {}", e);
			} catch (IllegalAccessException e) {
				log.error("IllegalAccessException {}", e);
			}
		}
		return handler;
	}
	
	public static Handler builderHandlerByFactory(Class<? extends HandlerFactory> clazz){
		HandlerFactory handler = null;
		try{
			handler = SpringUtils.getBean(clazz);
		}catch (BeansException e){
			log.info("{} bean not found, create it", clazz.getName());
		}
		if(handler == null){
			try {
				handler = clazz.newInstance();
			} catch (InstantiationException e) {
				log.error("InstantiationException {}", e);
				return null;
			} catch (IllegalAccessException e) {
				log.error("IllegalAccessException {}", e);
				return null;
			}
		}
		return handler.create();
	}
	
	public abstract Handler create();
}
